﻿@model String
@using Nop.Core
@using Nop.Services.Security
@using Nop.Web.Framework.UI;
@{
    //we do not bundle this script file (does not work for some reasons in bundle)
    Html.AddScriptParts("~/Content/tinymce/tinymce.min.js", true);
    
    var permissionService = EngineContext.Current.Resolve<IPermissionService>();
    var allowJbimages = false;
    var allowRoxyFileman = permissionService.Authorize(StandardPermissionProvider.HtmlEditorManagePictures);
    //TinyMCE picture browser does not work in a virtual category
    //so if you're running in a virtual directory, then uncomment the code below:
    //allowJbimages = true;
    //allowRoxyFileman =false;
    

    var random = CommonHelper.GenerateRandomInteger();
}
<script>
    $(document).ready(function () {
        @* wooncherk contribution *@
        function RoxyFileBrowser@(random)(field_name, url, type, win) {
            var roxyFileman = '@Url.Content("~/Content/Roxy_Fileman/index.html")';
            if (roxyFileman.indexOf("?") < 0) {
                roxyFileman += "?type=" + type;
            }
            else {
                roxyFileman += "&type=" + type;
            }
            roxyFileman += '&input=' + field_name + '&value=' + document.getElementById(field_name).value;
            tinyMCE.activeEditor.windowManager.open({
                file: roxyFileman,
                title: 'Roxy Fileman',
                width: 850,
                height: 650,
                resizable: "yes",
                plugins: "media",
                inline: "yes",
                close_previous: "no"
            }, { window: win, input: field_name });
            return false;
        }

        tinyMCE.init({
            selector: "#@ViewData.TemplateInfo.GetFullHtmlFieldId(string.Empty)",
            fontsize_formats: "8pt 9pt 10pt 11pt 12pt 26pt 36pt",
            height: 350,
            width: 790,
            plugins: [
                "advlist autolink lists link image charmap print preview anchor",
                "searchreplace visualblocks code fullscreen",
                "insertdatetime media table contextmenu paste@(allowJbimages ? " jbimages" : null)"
            ],
            toolbar: "insertfile undo redo | styleselect | fontselect | fontsizeselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image@(allowJbimages ? " jbimages" : null)",
            //"relative_urls" required by jbimages plugin to be set to "false"
            relative_urls: false,
            @if (allowRoxyFileman){
                <text>
                //picture manager
                    file_browser_callback: RoxyFileBrowser@(random),
                </text>
            }
            //we should set "convert_urls" set to "false" (because of "relative_urls" requirement)
            //otherwise, it'll break existing links when editing message template
            convert_urls: false
        });
    });
</script>
@Html.TextArea(string.Empty, /* Name suffix */
    ViewData.TemplateInfo.FormattedModelValue /* Initial value */
)